home *** CD-ROM | disk | FTP | other *** search
- Path: coranto.ucs.mun.ca!usenet
- From: saustin@terra.nlnet.nf.ca (Steve Austin)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q]Dereference within class - Possible?
- Date: Thu, 14 Mar 1996 23:43:42 GMT
- Organization: Kickham Productions
- Message-ID: <4iaav3$k53@coranto.ucs.mun.ca>
- References: <Do566t.F1H.0.queen@torfree.net>
- NNTP-Posting-Host: n104h130.nlnet.nf.ca
- X-Newsreader: Forte Agent .99b.112
-
- bh332@freenet.toronto.on.ca (Karim Ladha) wrote:
- >In normal C style, one can code the following example(C++ compiler);
- >
- >int main() {
- > int x = 1;
- > int &x1 = x;
- > ...
- >}
- >
- >Is it also possible to use the same syntax within a class without intializing
- >the referencer? Any information will be appreciated...
- >
-
- Karim, I think you're asking how to use references as class data
- members, is that right? If so, then yes, you can use similar syntax,
- but you *have* to use an initializer list, like so...
-
- class Example {
- public:
- Example(int&);
- private:
- int& x1;
- };
- Example::Example(int& x) : x1(x) {}
-
- The initializer list has to be used to initialize reference and const
- members; it can't be done explicitly in the constructor body, because
- they have to be initialized before the constructor starts up.
-
- Steve
-
-
-